﻿using DGIS.GscloudService.IService;
using DGIS.GscloudService.Service;
using DGIS.Model;
using HtmlAgilityPack;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace DGIS.Control
{
    public class GsCloudControl
    {
        /// <summary>
        /// 根据经纬度、时间查询影像
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="month"></param>
        /// <param name="startLon"></param>
        /// <param name="endLon"></param>
        /// <param name="startLat"></param>
        /// <param name="endLat"></param>
        /// <returns></returns>
        public List<GSCloud> QueryImgListByLonLat(DateTime startDate, DateTime endDate, string month, string productId, string datatype, string startLon, string endLon, string startLat, string endLat, bool showData, ref CookieContainer cookieContainer)
        {
            string polygonStr = string.Format("[{0},{1}],[{2},{3}],[{4},{5}],[{6},{7}]", startLon, startLat, endLon, startLat, endLon, endLat, startLon, endLat);
            return QueryImgListByPolygon(startDate, endDate, month, productId, datatype, polygonStr, showData, ref cookieContainer);
        }

        /// <summary>
        /// 根据行列号、时间查询影像
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="month"></param>
        /// <param name="startPath"></param>
        /// <param name="endPath"></param>
        /// <param name="startRow"></param>
        /// <param name="endRow"></param>
        /// <param name="cookieContainer"></param>
        /// <returns></returns>
        public List<GSCloud> QueryImgListByPathRow(DateTime startDate, DateTime endDate, string month, string productId, string datatype, string startPath, string endPath, string startRow, string endRow, bool showData, ref CookieContainer cookieContainer)
        {
            StringBuilder jsonSB = new StringBuilder();
            jsonSB.Append("tableInfo={\"offset\":0,\"pageSize\":1000,\"totalPage\":1,\"totalSize\":0,\"sortSet\":[{\"id\":\"datadate\",\"sort\":\"desc\"}],\"filterSet\":[]}&");            
            jsonSB.Append("query= {	\"productid\": {        \"in\": [\"" + productId + "\"]    },\"geom_params\": {        \"qtype\":4,\"min_path\":"+ startPath + ",\"max_path\":"+ endPath + ",\"min_row\":"+ startRow + ",\"max_row\":"+ endRow + "},\"path\":{\"gte\":"+startPath+",\"lte\":"+endPath+"},\"row\":{\"gte\":"+startRow+",\"lte\":"+endRow+"},	\"datadate\": {		\"gte\": " + startDate.ToString("yyyyMMdd") + ",		\"lte\": " + endDate.ToString("yyyyMMdd") + "	},\"dataexists\":1}");            
            jsonSB.Append("&queryFields= [\"dataid, productid, datadate, sensor, satellite, path, row, cloud_cover, dataexists\"]");

            return QueryImgList(jsonSB.ToString(), ref cookieContainer);
        }

        /// <summary>
        /// 根据自定义范围、时间查询影像
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="month"></param>
        /// <param name="polygonStr"></param>
        /// <returns></returns>
        public List<GSCloud> QueryImgListByPolygon(DateTime startDate, DateTime endDate, string month, string productId, string datatype, string polygonStr, bool showData, ref CookieContainer cookieContainer)
        {
            StringBuilder jsonSB = new StringBuilder();
            jsonSB.Append("tableInfo={\"offset\":0,\"pageSize\":1000,\"totalPage\":1,\"totalSize\":0,\"sortSet\":[{\"id\":\"datadate\",\"sort\":\"desc\"}],\"filterSet\":[]}&");
            //jsonSB.Append(string.Format("productId={0}&datatype={1}&", productId, datatype));

            jsonSB.Append("query= {	\"productid\": {        \"in\": [\""+productId+"\"]    },\"geom_params\": {        \"qtype\": 1,        \"value\": {                \"type\": \"Polygon\",			\"coordinates\": [                ["+polygonStr+"				]			]		}},	\"path\": {},	\"row\": {},	\"datadate\": {		\"gte\": "+ startDate.ToString("yyyyMMdd") + ",		\"lte\": "+ endDate.ToString("yyyyMMdd") + "	},\"dataexists\":1}");
//, ,polygonStr,,)
            //jsonSB.Append("data={");

            //if (showData)
            //    jsonSB.Append(string.Format("\"sdate\":\"{0}\",\"edate\":\"{1}\",",
            //       string.Format("{0:yyyy-MM-dd}", startDate), string.Format("{0:yyyy-MM-dd}", endDate)));
            //jsonSB.Append(string.Format("\"qtype\":1,\"wkt\":\"POLYGON(({0}))\"", polygonStr));

            ////jsonSB.Append(string.Format("\"sdate\":\"{0}\",\"edate\":\"{1}\",\"qtype\":1,\"wkt\":\"POLYGON(({2}))\"",
            ////    string.Format("{0:yyyy-MM-dd}", startDate), string.Format("{0:yyyy-MM-dd}", endDate), polygonStr));
            //if (month != "全部" && !string.IsNullOrEmpty(month))
            //    jsonSB.Append(string.Format(",\"months\":[\"{0}\"]", month));

            jsonSB.Append("&queryFields= [\"dataid, productid, datadate, sensor, satellite, path, row, cloud_cover, dataexists\"]");

            return QueryImgList(jsonSB.ToString(), ref cookieContainer);
        }

        private List<GSCloud> QueryImgList(string postDataStr, ref CookieContainer cookieContainer)
        {
            DGIS.GscloudService.IService.IGsCloudService gsCloudService = new DGIS.GscloudService.Service.GsCloudService();
            return gsCloudService.QueryGSCloudList(postDataStr, ref cookieContainer);
        }
        
        /// <summary>
        /// 获取影像真实下载地址
        /// </summary>
        /// <param name="dataId"></param>
        /// <param name="dataSetId"></param>
        /// <returns></returns>
        public string QueryImgDownloadUrl(string dataId, string dataSetId, ref CookieContainer cookieContainer)
        {
            DGIS.GscloudService.IService.IGsCloudService gsCloudService = new DGIS.GscloudService.Service.GsCloudService();
            return gsCloudService.QueryImgDownloadUrl(dataId, dataSetId, ref cookieContainer);
        }

        /// <summary>
        /// 获取数据类型集合
        /// </summary>
        /// <returns></returns>
        public List<GSDataSet> GetGSDataSetList()
        {
            DGIS.GscloudService.IService.IGsCloudService gsCloudService = new DGIS.GscloudService.Service.GsCloudService();
            return gsCloudService.GetGSDataSetList();
        }

        /// <summary>
        /// 更新本地数据类型集合
        /// </summary>
        /// <returns></returns>
        public bool UpdateGSDataSets()
        {
            DGIS.GscloudService.IService.IGsCloudService gsCloudService = new DGIS.GscloudService.Service.GsCloudService();
            List<GSDataSet> items = gsCloudService.QueryGSDataSetList();
            if (items.Any())
                return gsCloudService.AddGSDataSets(items);

            return false;
        }
    }
}
